//
//  KTBaseNavgationController.swift
//  KOLToken
//
//  Created by yangsan on 2020/2/26.
//  Copyright © 2020 iOS-team. All rights reserved.
//

import UIKit

class KTBaseNavgationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let bar = UINavigationBar.appearance()
        var dic: [NSAttributedString.Key : Any] = [:]
        dic.updateValue(UIColor.init(hex: 0x212121), forKey: .foregroundColor)
        dic.updateValue(UIFont.systemFont(ofSize: 16), forKey: .font)
        bar.isTranslucent = false
        bar.titleTextAttributes = dic
        self.delegate = self
    }
    
    
//    override var childForStatusBarStyle: UIViewController? {
//        return self.topViewController
//    }
//    override var childForStatusBarHidden: UIViewController? {
//        return self.topViewController
//    }

}

extension KTBaseNavgationController : UINavigationControllerDelegate {
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            guard var image = UIImage.init(named: "icon_nav_arrow_back") else { return }
            image = image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItem.Style.plain, target: self, action: #selector(popAction))
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        
        if viewController.shouldNavigationBarHidden != self.isNavigationBarHidden {
            self.setNavigationBarHidden(viewController.shouldNavigationBarHidden, animated: animated)
        }
    }
    
    @objc public func popAction() {
        popViewController(animated: true)
    }
}

